<?php /* DEPARTMENTS $Id: addedit.php,v 1.24 2005/04/08 13:41:51 gregorerhardt Exp $ */
// Add / Edit Company
$dept_id = isset($_GET['dept_id']) ? $_GET['dept_id'] : 0;
$company_id = isset($_GET['company_id']) ? $_GET['company_id'] : 0;

// check permissions for this department
$canEdit = !getDenyEdit( $m, $dept_id );
if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}

// pull data for this department
$q  = new DBQuery;
$q->addTable('departments','dep');
$q->addQuery('dep.*, company_name');
$q->addJoin('companies', 'com', 'com.company_id = dep.dept_company');
$q->addWhere('dep.dept_id = '.$dept_id);
$sql = $q->prepare();
$q->clear();
if (!db_loadHash( $sql, $drow ) && $dept_id > 0) {
	$titleBlock = new CTitleBlock( 'Invalid Department ID' );
	$titleBlock->addButton('companies list', '?m=companies');
	$titleBlock->show();
} else {
	$creator = isset($drow['dept_owner']) ? $drow['dept_owner'] : 1;
	if ($AppUI->user_id != 1 && $AppUI->user_id != $creator) {
		$AppUI->setMsg('Access denied', UI_MSG_ERROR);
		$AppUI->redirect();
	}

	##echo $sql.db_error();##
	$company_id = $dept_id ? $drow['dept_company'] : $company_id;

	// check if valid company
	$q  = new DBQuery;
	$q->addTable('companies','com');
	$q->addQuery('company_name');
	$q->addWhere('com.company_id = '.$company_id);
	$sql = $q->prepare();
	$q->clear();
	$company_name = db_loadResult( $sql );
	if (!$dept_id && $company_name === null) {
		$AppUI->setMsg( 'badCompany', UI_MSG_ERROR );
		$AppUI->redirect();
	}

	// collect all the departments in the company
	$depts = array( 0 => '' );
	if ($company_id) {
		$q  = new DBQuery;
		$q->addTable('departments','dep');
		$q->addQuery('dept_id, dept_name, dept_parent');
		$q->addWhere('dep.dept_company = '.$company_id);
		$q->addWhere('dep.dept_id != '.$dept_id);
		$depts = $q->loadArrayList();
		$depts['0']  = array( 0, '- '.$AppUI->_('Select Unit').' -', -1 );
	}

	// collect all the users for the department owner list
	$q  = new DBQuery;
	$q->addTable('users','u');
	$q->addTable('contacts','con');
	$q->addQuery('user_id');
	$q->addQuery('CONCAT_WS(" ",contact_first_name, contact_last_name)'); 
	$q->addOrder('contact_first_name');
	$q->addWhere('u.user_contact = con.contact_id');
	$q->addOrder('contact_last_name, contact_first_name');
	$owners = arrayMerge( array( '0'=>'' ), $q->loadHashList() );

	// setup the title block
	$ttl = $dept_id > 0 ? 'Edit Department' : 'Create Department';
	$titleBlock = new CTitleBlock( $ttl );
	$titleBlock->addButton('companies list', '?m=companies' );
	$titleBlock->show();
?>
<script language="javascript">
function testURL( x ) {
	var test = "document.editFrm.dept_url.value";
	test = eval(test);
	if (test.length > 6) {
		newwin = window.open( "http://" + test, 'newwin', '' );
	}
}

function submitIt() {
	var form = document.editFrm;
	if (form.dept_name.value.length < 2) {
		alert( '<?php echo $AppUI->_( 'deptValidName', UI_OUTPUT_JS );?>' );
		form.dept_name.focus();
	} else {
		form.submit();
	}
}

function doGetDepartmentList() {
	DepartmentList(<?=$company_id?>, function() {initTree($("idDeptTree"));} ).show();
}

function doSelectDepartment(id)
{
	$('idDeptId').value = id;
	$('idDeptName').value = unescape($('idDept['+id+']').getAttribute('value'));
	$('idGlass').onclick();
}

</script>

<form name="editFrm" action="?m=departments" method="post">
	<input type="hidden" name="dosql" value="do_dept_aed" />
	<input type="hidden" name="dept_id" value="<?php echo $dept_id;?>" />
	<input type="hidden" name="dept_company" value="<?php echo $company_id;?>" />

<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td class="tabon">Department information</td>
<td width="100%">&nbsp;</td>
</tr>
</table>
<table cellspacing="0" cellpadding="7" border="0" width="100%" class="infopanel">
<tr>
<td>
	<input type="button" value="<?=$AppUI->_( 'Save' )?>" class="button" onClick="submitIt()"/>
	<input type="button" value="<?=$AppUI->_( 'Cancel' )?>" class="button" onClick="history.back(-1)" />
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="subinfopanel">
<tr>
	<td align="right" width="120px"><?=$AppUI->_( 'Company' )?></td>
	<td><div class="text" style="width: 400px"><a href="javascript:Company(<?=$company_id?>).show()"><b><?=$company_name?></b></a></div></td>
</tr>
<?	if (count( $depts )) { ?>
<tr>
	<td align="right" nowrap><?=$AppUI->_( 'Parent department' )?></td>
	<td>
		<input id="idDeptId" type="hidden" name="dept_parent" value="<?=@$drow['dept_parent']?>"/>
		<input id="idDeptName" type="text" class="text" style="width: 200px" value="<?=$depts[@$drow['dept_parent']][1]?>"/>
		<img src="images/treetable.gif" class="ico clickable" onclick="doGetDepartmentList()"/>
	</td>
</tr>
<? } else { ?>
	<input type="hidden" name="dept_parent" value="0">
<? } ?>
<? if ($AppUI->user_id == 1) { ?>
<tr>
	<td align="right"><?=$AppUI->_( 'Owner' )?></td>
	<td><?=arraySelect( $owners, 'dept_owner', 'size="1" class="text" style="width: 150px"', $creator )?>
	</td>
</tr>
<? } ?>
<tr>
	<td align="right" nowrap><?=$AppUI->_( 'Name' )?></td>
	<td>
		<input type="text" class="text" name="dept_name" value="<?=@$drow['dept_name']?>"
			style="width: 400px" maxlength="255" />
		<span title="Required" style="cursor: default">(<?=$AppUI->_( '*' )?>)</span>
	</td>
</tr>
<tr>
	<td align="right" nowrap><?=$AppUI->_( 'Phone' )?></td>
	<td>
		<input type="text" class="text" name="dept_phone" style="width: 178px"
			value="<?php echo @$drow['dept_phone'];?>" maxlength="30"/>
		&nbsp;<?=$AppUI->_( 'Fax' )?>&nbsp;
		<input type="text" class="text" name="dept_fax" style="width: 178px"
			value="<?=@$drow['dept_fax']?>" maxlength="30" />
	</td>
</tr>
<tr>
	<td align="right" valign="top"><?=$AppUI->_( 'Address' )?></td>
	<td><textarea class="text" name="dept_address1" style="width: 400px" rows="2" maxlength="512"><?=@$drow['dept_address1']?></textarea></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_( 'City' )?></td>
	<td><input type="text" class="text" name="dept_city"
			value="<?=@$drow['dept_city']?>" style="width: 132px" maxlength="50">
		&nbsp;<?=$AppUI->_( 'State' )?>&nbsp;
		<input type="text" class="text" name="dept_state"
			value="<?=@$drow['dept_state']?>" style="width: 125px" maxlength="50">
		&nbsp;<?=$AppUI->_( 'Zip' )?>&nbsp;
		<input type="text" class="text" name="dept_zip"
			value="<?=@$drow['dept_zip']?>" style="width: 50px" maxlength="15">
	</td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_( 'URL' )?><A name="x"></a></td>
	<td>
		<input type="text" class="text" value="<?=@$drow['dept_url']?>" name="dept_url" style="width: 400px" maxlength="255">
		<a href="#x" onClick="testURL('dept_url')">[<?=$AppUI->_( 'test' )?>]</a>
	</td>
</tr>
<tr>
	<td align="right" valign="top" nowrap><?=$AppUI->_( 'Description' )?></td>
	<td align="left">
		<textarea style="width: 400px" rows="5" class="textarea" name="dept_desc"><?=@$drow['dept_desc']?></textarea>
	</td>
</tr>
</table>
</form>
<?php } ?>
